﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using System.Linq;
using Wjtour.Domain;
using Wjtour.BLL;

namespace Wjtour.Web.Front
{
    public partial class photographynew : MemberBasePage
    {
        protected CuteWebUI.Uploader Uploader1;
        protected Repeater rptPreview;
        protected TextBox title;
        protected Button submit;

        protected int id = 0;
        Wjtour.Domain.gallery link = new Domain.gallery();

        private gallery_bll _gallery_bll = new gallery_bll();
        private Wjtour.BLL.sitepicture_bll _bll = new BLL.sitepicture_bll();
        sitepicture_bll _sitepicture_bll = new sitepicture_bll();
        
        private static List<Wjtour.Domain.dto.sitepicture_picturetype> tempPictures = new List<Wjtour.Domain.dto.sitepicture_picturetype>();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            Uploader1.UploadCompleted += Uploader1_UploadCompleted;
            if (!IsPostBack)
            {
                tempPictures.Clear();
            }
            //if (id > 0)
            //{
            //    link = _gallery_bll.Load(id);
            //    tempPictures = _gallery_bll.GetGalleryPicturesList(id);
            //    bindRepeater(tempPictures);

            //    if (!IsPostBack)
            //    {
            //        if (link != null)
            //        {
            //            title.Text = link.title;
            //        }
            //    }
            //}
        }

        void Uploader1_UploadCompleted(object sender, CuteWebUI.UploaderEventArgs[] args)
        {
            args.ToList().ForEach(a =>
            {
                string filename = GetFileName("time");
                a.CopyTo("~/uploads/" + filename + ".jpg");
                a.Delete();
                var sp = new Wjtour.Domain.dto.sitepicture_picturetype
                {
                    picturepath = "/uploads/" + filename + ".jpg",
                    picturetitle = filename,
                    iscover = 0
                };
                var smallImg = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename + ".jpg", 100, 73, "jpg");
                sp.smallImg = smallImg;
                tempPictures.Add(sp);
            });
            bindRepeater(tempPictures);
        }

        void bindRepeater(object datasource)
        {
            rptPreview.DataSource = datasource;
            rptPreview.DataBind();
        }

        void submit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(title.Text))
            {
                Utils.Alert(this.Page, "图片标题不能为空！");
                return;
            }
            link.id = id;
            link.title = title.Text;
            link.picturetypeid = 5;
            bool b = false;

            if (link.id == 0)
            {
                link.createdate = DateTime.Now;
                b = _gallery_bll.Save(link, out id);

                if (tempPictures.Count > 0)
                {
                    foreach (var item in tempPictures)
                    {
                        var sitepic = new sitepicture();
                        sitepic.picturepath = item.picturepath;
                        sitepic.picturetitle = item.picturetitle;
                        sitepic.smallImg = item.smallImg;
                        sitepic.createdate = DateTime.Now;
                        sitepic.iscover = 0;
                        sitepic.isactive = 1;
                        sitepic.isaudit = 1;
                        sitepic.galleryid = id;
                        sitepic.picturetypeid = 5;
                        sitepic.memberid = CurrentMember.id;
                        _sitepicture_bll.Save(sitepic);
                    }
                }
                tempPictures.Clear();
                Utils.Redirect(this.Page, "photography.aspx", false);
            }
            else
            {
                b = _gallery_bll.Save(link);
            }
            if (b)
            {
                Utils.Alert(this.Page, "保存成功！");
                tempPictures.Clear();
                Utils.Redirect(this.Page, "photography.aspx", false);
            }
        }
    }
}
